package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpBMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.share.ShareCUserInfo;

public class BAddZhifubaoActivity extends AbActivity {

    private EditText edtName, edtNum;
    private Button btnSave;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_add_zhifubao);
        edtName = (EditText) findViewById(R.id.edt_cshenqing_add_zhifubao_name);
        edtNum = (EditText) findViewById(R.id.edt_cshenqing_add_zhifubao_num);
        btnSave = (Button) findViewById(R.id.btn_cshenqing_add_zhifubao_save);
    }

    @Override
    protected void initData() {
        setTitleBar("支付宝账户");
        setLeftView();
        getZHifubaoMessage();
    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
    }

    public void save() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpBMethods.saveZhifubao(listener,
                shareCUserInfo.getId(), edtName.getText().toString(),
                edtNum.getText().toString());
    }

    //获取支付宝信息
    public void getZHifubaoMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpBMethods.getZhifubao(listener,
                shareCUserInfo.getId());
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BAddZhifubaoActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(BAddZhifubaoActivity.this, jsonObject.getString("message"));
                            setResult(RESULT_CANCELED);
                            finish();
                        } else {
                            ToastUtil.showShort(BAddZhifubaoActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    String results = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(results);
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BAddZhifubaoActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject content = jsonObject.getJSONObject("content");
                            edtName.setText(content.getString("alipay_realname"));
                            edtNum.setText(content.getString("alipay_account"));
                        } else {
                            ToastUtil.showShort(BAddZhifubaoActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
}
